/*
 * @Author: muge
 * @Date: 2025-06-27 15:25:17
 * @LastEditors: muge
 * @LastEditTime: 2025-06-27 15:32:06
 */

/**
 * 设备信息接口
 */
export type DeviceInfoType = 'pc' | 'mobile';
export interface DeviceInfo {
  type: DeviceInfoType;
  width: number;
  height: number;
  isMobile: boolean;
  userAgent: string;
}

/**
 * 检测设备类型和屏幕分辨率
 * @returns DeviceInfo 设备信息对象
 */
export const detectDevice = (): DeviceInfo => {
  // 获取用户代理字符串
  const userAgent = navigator.userAgent.toLowerCase();

  // 获取屏幕分辨率
  const width = window.screen.width;
  const height = window.screen.height;

  // 检测是否为移动设备
  const isMobileDevice =
    /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(
      userAgent,
    );

  // 判断设备类型：移动设备或屏幕宽度小于等于750px都认为是手机端
  const isMobile = isMobileDevice || width <= 750;

  return {
    type: isMobile ? 'mobile' : 'pc',
    width,
    height,
    isMobile,
    userAgent,
  };
};
